package com.yczlab.mybatis;

import com.yczlab.mybatis.dao.EmployeeDaoDynamicSql;
import com.yczlab.mybatis.entity.Employee;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class EmployeeDynamicSqlTset {

    @Autowired
    private EmployeeDaoDynamicSql empDySqlDao;

    @Test
    public void test() {
        System.out.println("Hello Spring");
    }

    @Test
    public void getEmpsByConditionIfTest() {
        Map<String, Object> map = new HashMap<>();
        //map.put("empId", 123);
        map.put("lastName", "%e%");
        map.put("firstName", "%a%");
        map.put("depId", 50);
        List<Employee> emps = empDySqlDao.getEmpsByConditionIf(map);
        emps.forEach(System.out::println);
    }

    @Test
    public void getEmpsByConditionTrimTest() {
        Map<String, Object> map = new HashMap<>();
        //map.put("empId", 123);
        map.put("lastName", "%e%");
        map.put("firstName", "%a%");
        //map.put("depId", 50);
        List<Employee> emps = empDySqlDao.getEmpsByConditionTrim(map);
        emps.forEach(System.out::println);
    }

    @Test
    public void getEmpsByConditionChooseTest(){
        Map<String, Object> map = new HashMap<>();
        //map.put("empId", 123);
        map.put("lastName", "%chen%");
        map.put("firstName", "%a%");
        List<Employee> emps = empDySqlDao.getEmpsByConditionChoose(map);
        emps.forEach(System.out::println);
    }

    @Test
    public void updateEmpTest() {
        Employee employee = new Employee();
        int empId = 209;
        employee.setEmployeeId(empId);
        employee.setLastName("yczlab");
        employee.setFirstName("yczlab");
        boolean flag = empDySqlDao.updateEmp(employee);
        if (flag) {
            System.out.println("对编号为 " + empId + " 的员工信息更新成功");
            System.out.println(employee);
        }
    }

    @Test
    public void getEmpsByConditionForeachTest() {
        List<Integer> empIds = Arrays.asList(100, 123, 156, 209);
        List<Employee> emps = empDySqlDao.getEmpsByConditionForeach(empIds);
        emps.forEach(System.out::println);
    }

    @Test
    public void addEmpsTest() {
        List<Employee> emps = empDySqlDao.getEmpsByConditionForeach(Arrays.asList(123, 125, 127));
        for (int i = 0; i < emps.size(); i++) {
            Employee emp = emps.get(i);
            emp.setLastName("New" + (i + 1));
            emp.setFirstName("add" + (i + 1));
        }
        boolean flag = empDySqlDao.addEmps(emps);
        if (flag) {
            System.out.println("批量添加员工成功！");
            emps.forEach(System.out::println);
        }
    }

    @Test
    public void getAllEmpsInnerParam() {
        List<Employee> emps = empDySqlDao.getAllEmpsInnerParam();
        emps.forEach(System.out::println);
    }
}
